package com.summba.corpus.extractor;

import java.io.BufferedReader;
import java.util.ArrayList;
//import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
//import java.util.Map;
//import java.util.Map.Entry;

import com.summba.analysis.util.WeiboMessage;
import com.summba.common.Common;

public class KeywordExtractor {
	
	private String dbFile;
	private String corpusFile;
	private List<String> corpusList = new ArrayList<String>();
	//private Map<String,String> corpusMap = new HashMap<String,String>();
	private List< WeiboMessage > resList = new ArrayList<WeiboMessage>();
	//private 
	
	public KeywordExtractor( String corpusFlie , String saveFile){
		dbFile = saveFile;
		corpusFile = corpusFlie;
		if( Common.checkFileExistAndCreate(dbFile) ){
			System.out.println("Success create dbFile!");
		}else{
			System.out.println("Please check the dbFile!");
		}
		System.out.println("corpusFile : "+corpusFile);
		BufferedReader tempBR = Common.getFileInfo(corpusFile);
		corpusList = Common.bufferedReaderToList(tempBR);
		System.out.println("corpusList Size: "+corpusList.size());
		//corpusFileList = Common.getFloderFileList(corpusFilePathTemp);
	}
	
	public void getTextContent(String Keyword)throws Exception{
		String matchKeyword = "keyword:" + Keyword;
		boolean checkStatus = false;
		String strKeyword = "";
		System.out.println("getTextContent is Start");
		for (Iterator<String> contentIterator = corpusList.iterator(); contentIterator.hasNext();) {
			String string =  contentIterator.next();
			//System.out.println("!!"+string);
			if( string.indexOf(matchKeyword,0) != -1 ){
				checkStatus = true;
				strKeyword = string;
				//System.out.println("keyword: "+string);
			}
			if( string.indexOf("text:",0) != -1 ){
				if( checkStatus ){
					WeiboMessage weioMsg = new WeiboMessage();
					weioMsg.setKeyword(strKeyword);
					weioMsg.setText(string);
					//System.out.println("text: "+string);
					resList.add(weioMsg);
					checkStatus = false;
					strKeyword = "";
				}
			}
		}
	}
	
	public void GenFile(){
		
		//List<Map.Entry<String, Integer>> infoIds = 
			//new ArrayList< Map.Entry<String, Integer> >(wordFrequencyCount.entrySet());
		for (int j = 0; j < resList.size(); j++) {
			WeiboMessage aa =  resList.get(j);
			Common.appendFile(dbFile, aa.getKeyword()+ "\n");
			Common.appendFile(dbFile, aa.getText()+ "\n");
			Common.appendFile(dbFile, "==========================================" +
					"========"+ "\n");
		}
		
		
		
	}

}
